<?php

/**
 * This is the model class for table "settlements".
 *
 * The followings are the available columns in table 'settlements':
 * @property integer $id
 * @property string $name
 * @property integer $x
 * @property integer $y
 * @property integer $population
 * @property integer $approval
 * @property integer $id_user
 * @property integer $iscapital
 */
class Settlements extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Settlements the static model class
	 */
	
	public $maxResources = array();
	
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'settlements';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('name, x, y, population, approval, id_user, iscapital', 'required'),
			array('x, y, population, approval, id_user, iscapital', 'numerical', 'integerOnly'=>true),
			array('name', 'length', 'min'=>3, 'max'=>15),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, name, x, y, population, approval, id_user, iscapital', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'fields' => array(self::HAS_MANY, 'Fields', 'id_settlement'),
			'resources' => array(self::HAS_MANY, 'Resources', 'id_settlement'),
			'buildings' => array(self::HAS_MANY, 'Buildings', 'id_settlement'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'name' => 'Название деревни',
			'x' => 'X',
			'y' => 'Y',
			'population' => 'Население',
			'approval' => 'Одобрение',
			'id_user' => 'Id User',
			'iscapital' => 'Iscapital',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('name',$this->name,true);
		$criteria->compare('x',$this->x);
		$criteria->compare('y',$this->y);
		$criteria->compare('population',$this->population);
		$criteria->compare('approval',$this->approval);
		$criteria->compare('id_user',$this->id_user);
		$criteria->compare('iscapital',$this->iscapital);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
	
	
	public function maxResourceCount()
	{
		$user = User::model()->findByPk(Yii::app()->user->getId());
		$settlement = $user->settlement;
		$rtypes = ResourcesType::model()->findAll();
		
		foreach($rtypes as $rt)
		{
			$buildings = Buildings::model()->findAll('id_settlement = :id_s AND id_building = :id_b',
													  array(':id_s'=>$settlement->id,':id_b'=>$rt->id_building_type)
													);
			$this->maxResources[$rt->id][0] = 0;
			$this->maxResources[$rt->id][1] = $rt->name;
			foreach($buildings as $building)
			{		
				$this->maxResources[$rt->id][0] += 1000*$building->lvl;
			}
		}
	}
	
	
	protected function afterFind()
	{
		if($this->hasEventHandler('onAfterFind'))
			$this->onAfterFind(new CEvent($this));
	}
	
}